﻿//Looping search by HS
function searchHS(proCateID, proCateCode, ProCateDesc) {
    $("input[jns=txtProCateID]")[0].value = proCateID;
    $("input[jns=txtProCateCode]")[0].value = proCateCode;
    $("input[jns=txtUserInput]")[0].value = proCateCode + " - " + ProCateDesc;
    $("input[jns=btnSearch]").click();
}

//Looping search by Keywords
function searchKeywords(keywords) {
    $("input[jns=txtUserInput]")[0].value = keywords;
    $("input[jns=btnSearch]").click();
}

//Enter event when user press enter
function checkForEnter(event) {
    if (event.keyCode == 13) {
        //Call button search click
        $("input[jns=btnSearch]").click();
        //Disabled submit
        event.preventDefault();
        return false;
    }
}


$(document).ready(function () {

    //Set focus and Hint to User Input TextBox
    $("input[jns=txtUserInput]").inputHints();
    $("input[jns=txtUserInput]").focus();
    
    //User Input TextBox get focus
    //    $("input[jns=txtUserInput]").focusin(function () {
    //        if ($("input[jns=txtUserInput]").val() == "" || $("input[jns=txtUserInput]").val() == "Vui lòng nhập!") {
    //            $("input[jns=txtUserInput]").val("");
    //            $("input[jns=txtUserInput]").removeClass("required");
    //        }
    //    });
    //User Input TextBox lost focus
    //    $("input[jns=txtUserInput]").focusout(function () {
    //        if ($("input[jns=txtUserInput]").val() == "Vui lòng nhập!") {
    //            $("input[jns=txtUserInput]").val("");
    //            $("input[jns=txtUserInput]").removeClass("required");
    //        }
    //    });


    //Bypass AutoComplete
    $("img[jns=applyInput]").click(function () {
        $("input[jns=btnSearch]").click();
    });

    //Clear Input
    $("img[jns=clearInput]").click(function () {
        $("input[jns=txtUserInput]").val("");
        $("input[jns=txtUserInput]").removeClass("required");
    });

    //Auto Complete Text Box Search
    $("input[jns=txtUserInput]").autocomplete("GetProductHandler.ashx", {
        minChars: 2,
        cacheLength: 0,
        mustMatch: true,
        max: 20
        })
        .result(function (event, item) {
            if (item != undefined) {
                $("input[jns=txtProID]")[0].value = item[1];
                $("input[jns=txtProCateID]")[0].value = item[2];
                $("input[jns=txtProCode]")[0].value = item[3];
                $("input[jns=txtProCateCode]")[0].value = item[4];
            }
            else {
                $("input[jns=txtProID]")[0].value = '';
                $("input[jns=txtProCateID]")[0].value = '';
                $("input[jns=txtProCode]")[0].value = '';
                $("input[jns=txtProCateCode]")[0].value = '';
            }
        });

    //Flush cache autocomplete
    $("input[jns=txtUserInput]").flushCache();


    //Check whether user press enter key
    if ($.browser.mozilla)//for mozilla series browsers
        $("input[jns=txtUserInput]").keypress(checkForEnter);
    else //... and other
        $("input[jns=txtUserInput]").keydown(checkForEnter);

    
    //User hit Search button
    $("input[jns=btnSearch]").click(function () {
        if ($("input[jns=txtUserInput]").val() == "" || $("input[jns=txtUserInput]").val() == "Vui lòng nhập!") {
            return false; //If User Input emty return false
        }

        var proid = $("input[jns=txtProID]")[0].value;
        var procateid = $("input[jns=txtProCateID]")[0].value;
        //The TextSearch keywork MUST BE ENCODE before send
        var textsearch = encodeURIComponent($("input[jns=txtUserInput]")[0].value);

        //show ajax loader
        $("#ajaxloader").show();
        $("#divResult").hide();
        $("#divTableProductCategories").hide();
        $("#divTableProducts").hide();
        $("#divNoRecord").hide();
        $("#divDYM").hide();
        //ajax function return the searching result
        $.ajax({ type: "POST",
            url: "jFetchingResult.aspx?ProductID=" + proid + "&ProductCategoryID=" + procateid + "&TextSearch=" + textsearch,
            success: function (serverResponseData) // On success, having result
            {
                //hide ajaxloader
                $("#ajaxloader").hide();

                /*
                The Scripts below calculate and show Div Result
                */
                $("#divResult").fadeIn(300);
                var dtItems = eval("(" + serverResponseData + ")"); // evaluate retrived data to javascript object

                /*
                Show DIV No Records if:
                - Have no ProductCategories found
                - Have no Products found
                - Have no DYM's keywords found
                */
                if (dtItems.NewDataSet.Products == null & dtItems.NewDataSet.ProductCategories == null) {
                    $("#NoRecordKeyWord").html($("input[jns=txtUserInput]").val());
                    $("#divNoRecord").show();
                }

                /*
                DRAWING TABLE ProductCategories
                */
                var htmlProductCategoriesTable = "";
                if (dtItems.NewDataSet.ProductCategories != null) {
                    htmlProductCategoriesTable += "<tr style='background-color:#b7ddf2;font-weight:bold;'><td width='20%'>Mã HS</td><td width='80%'>Mô tả</td></tr>";
                    //Display divTableProducts
                    $("#divTableProductCategories").show();

                    if (dtItems.NewDataSet.ProductCategories.length == undefined) {//ProductCategories has only 1 row
                        var id = dtItems.NewDataSet.ProductCategories.ProductCategoryID;
                        var code = dtItems.NewDataSet.ProductCategories.ProductCategoryCode;
                        var desc = dtItems.NewDataSet.ProductCategories.Description;
                        var desc1 = '"' + desc + '"';
                        htmlProductCategoriesTable += "<tr>";
                        htmlProductCategoriesTable += "<td><a class='seasrchHS' onclick='searchHS(" + id + ",this.name," + desc1 + ");' name='" + code + "'>" + code + "</a></td>";
                        htmlProductCategoriesTable += "<td>" + desc + "</td>";
                        htmlProductCategoriesTable += "</tr>";
                        $("#countProductCategoriesResult").html("1");
                        $("#lblProductCategoriesOwn").html($("input[jns=txtUserInput]").val());
                    }
                    else { //ProductCategories has more than 1 row
                        //drawing ProductCategories table
                        for (var i = 0; i < dtItems.NewDataSet.ProductCategories.length; i++) // traverse through items in this object
                        {
                            var id = dtItems.NewDataSet.ProductCategories[i].ProductCategoryID;
                            var code = dtItems.NewDataSet.ProductCategories[i].ProductCategoryCode;
                            var desc = dtItems.NewDataSet.ProductCategories[i].Description;
                            var desc1 = '"' + desc + '"';

                            //                            if (i % 2 == 0)
                            //                                htmlProductCategoriesTable += "<tr style='background-color:white;'>";
                            //                            else
                            //                                htmlProductCategoriesTable += "<tr>";
                            htmlProductCategoriesTable += "<td><a class='seasrchHS' onclick='searchHS(" + id + ",this.name," + desc1 + ");' name='" + code + "'>" + code + "</a></td>";
                            htmlProductCategoriesTable += "<td>" + desc + "</td>";
                            htmlProductCategoriesTable += "</tr>";
                        }
                        $("#countProductCategoriesResult").html(dtItems.NewDataSet.ProductCategories.length);
                        $("#lblProductCategoriesOwn").html($("input[jns=txtUserInput]").val());
                    }
                }
                else {
                    /*
                    Show DYM results
                    */
                    if (dtItems.NewDataSet.DYM != null) {
                        $("#divDYM").show();
                        $("#LinkDYMResult").html(dtItems.NewDataSet.DYM.DYM_Words);
                        $("#LinkDYMResult").attr("name", dtItems.NewDataSet.DYM.DYM_Words);
                    }
                }
                $("#_mtableProductCategories").html(htmlProductCategoriesTable);

                /*
                DRAWING TABLE Products
                */
                var htmlProductTable = "";
                if (dtItems.NewDataSet.Products != null) {
                    //Display divTableProductCategories
                    $("#divTableProducts").show();
                    htmlProductTable += "<tr style='background-color:#b7ddf2;font-weight:bold;'>";
                    htmlProductTable += "<td width='10%'>Mã HS</td>";
                    htmlProductTable += "<td width='20%'>Mô tả</td>";
                    htmlProductTable += "<td>ĐVT</td>";
                    htmlProductTable += "<td>Ưu đãi</td>";
                    htmlProductTable += "<td>TT</td>";
                    htmlProductTable += "<td>CEPT</td>";
                    htmlProductTable += "<td>ACFTA/TS</td>";
                    htmlProductTable += "<td width='10%'>Nước không áp dụng\nACFTA</td>";
                    htmlProductTable += "<td>AKFTA/TS</td>";
                    htmlProductTable += "<td width='10%'>Nước không áp dụng\nAKFTA</td>";
                    htmlProductTable += "<td>VJEPA</td>";
                    htmlProductTable += "<td>AANZFTA</td>";
                    htmlProductTable += "<td>AIFTA 2010</td>";
                    htmlProductTable += "<td>AIFTA 2011</td>";
                    htmlProductTable += "<td>VAT</td>";
                    htmlProductTable += "</tr>";
                    if (dtItems.NewDataSet.Products.length == undefined) {//If Products has only 1 row
                        var id = dtItems.NewDataSet.Products.ProductID;
                        var code = dtItems.NewDataSet.Products.ProductCode;
                        var desc = dtItems.NewDataSet.Products.Description;

                        var ACFTA_NoneApply = "";
                        if (dtItems.NewDataSet.Products.ACFTA_NoneApply != undefined)
                            ACFTA_NoneApply = dtItems.NewDataSet.Products.ACFTA_NoneApply;

                        var AKFTA_NoneApply = "";
                        if (dtItems.NewDataSet.Products.AKFTA_NoneApply != undefined)
                            ACFTA_NoneApply = dtItems.NewDataSet.Products.AKFTA_NoneApply;

                        var Unit = "";
                        if (dtItems.NewDataSet.Products.Unit != undefined)
                            Unit = dtItems.NewDataSet.Products.Unit;

                        var VAT = "";
                        if (dtItems.NewDataSet.Products.VAT != undefined)
                            VAT = dtItems.NewDataSet.Products.VAT;

                        htmlProductTable += "<tr style='background-color:white;'>";
                        htmlProductTable += "<td>" + code + "</td>";
                        htmlProductTable += "<td>" + desc + "</td>";
                        htmlProductTable += "<td>" + Unit + "</td>";
                        htmlProductTable += "<td>" + dtItems.NewDataSet.Products.Special + "</td>";
                        htmlProductTable += "<td>" + dtItems.NewDataSet.Products.TT + "</td>";
                        htmlProductTable += "<td>" + dtItems.NewDataSet.Products.CEPT + "</td>";
                        htmlProductTable += "<td>" + dtItems.NewDataSet.Products.ACFTA_TS + "</td>";
                        htmlProductTable += "<td>" + ACFTA_NoneApply + "</td>";
                        htmlProductTable += "<td>" + dtItems.NewDataSet.Products.AKFTA_TS + "</td>";
                        htmlProductTable += "<td>" + AKFTA_NoneApply + "</td>";
                        htmlProductTable += "<td>" + dtItems.NewDataSet.Products.VJEPA + "</td>";
                        htmlProductTable += "<td>" + dtItems.NewDataSet.Products.AANZFTA + "</td>";
                        htmlProductTable += "<td>" + dtItems.NewDataSet.Products.AIFTA_2010 + "</td>";
                        htmlProductTable += "<td>" + dtItems.NewDataSet.Products.AIFTA_2011 + "</td>";
                        htmlProductTable += "<td>" + VAT + "</td>";
                        htmlProductTable += "</tr>";
                        //display count products
                        $("#countProductsResult").html("1");
                        $("#lblProductsOwn").html($("input[jns=txtUserInput]").val());
                    }
                    else //If Products has more than 1 row
                    {
                        for (var i = 0; i < dtItems.NewDataSet.Products.length; i++) // traverse through items in this object
                        {
                            var id = dtItems.NewDataSet.Products[i].ProductID;
                            var code = dtItems.NewDataSet.Products[i].ProductCode;
                            var desc = dtItems.NewDataSet.Products[i].Description;
                            var ACFTA_NoneApply = "";
                            if (dtItems.NewDataSet.Products[i].ACFTA_NoneApply != undefined)
                                ACFTA_NoneApply = dtItems.NewDataSet.Products[i].ACFTA_NoneApply;

                            var AKFTA_NoneApply = "";
                            if (dtItems.NewDataSet.Products[i].AKFTA_NoneApply != undefined)
                                ACFTA_NoneApply = dtItems.NewDataSet.Products[i].AKFTA_NoneApply;

                            var Unit = "";
                            if (dtItems.NewDataSet.Products[i].Unit != undefined)
                                Unit = dtItems.NewDataSet.Products[i].Unit;

                            var VAT = "";
                            if (dtItems.NewDataSet.Products[i].VAT != undefined)
                                VAT = dtItems.NewDataSet.Products[i].VAT;

                            if (i % 2 == 0)
                                htmlProductTable += "<tr style='background-color:white;'>";
                            else
                                htmlProductTable += "<tr>";
                            htmlProductTable += "<td>" + code + "</td>";
                            htmlProductTable += "<td>" + desc + "</td>";
                            htmlProductTable += "<td>" + Unit + "</td>";
                            htmlProductTable += "<td>" + dtItems.NewDataSet.Products[i].Special + "</td>";
                            htmlProductTable += "<td>" + dtItems.NewDataSet.Products[i].TT + "</td>";
                            htmlProductTable += "<td>" + dtItems.NewDataSet.Products[i].CEPT + "</td>";
                            htmlProductTable += "<td>" + dtItems.NewDataSet.Products[i].ACFTA_TS + "</td>";
                            htmlProductTable += "<td>" + ACFTA_NoneApply + "</td>";
                            htmlProductTable += "<td>" + dtItems.NewDataSet.Products[i].AKFTA_TS + "</td>";
                            htmlProductTable += "<td>" + AKFTA_NoneApply + "</td>";
                            htmlProductTable += "<td>" + dtItems.NewDataSet.Products[i].VJEPA + "</td>";
                            htmlProductTable += "<td>" + dtItems.NewDataSet.Products[i].AANZFTA + "</td>";
                            htmlProductTable += "<td>" + dtItems.NewDataSet.Products[i].AIFTA_2010 + "</td>";
                            htmlProductTable += "<td>" + dtItems.NewDataSet.Products[i].AIFTA_2011 + "</td>";
                            htmlProductTable += "<td>" + VAT + "</td>";
                            htmlProductTable += "</tr>";
                        }
                        //display count products
                        $("#countProductsResult").html(dtItems.NewDataSet.Products.length);
                        $("#lblProductsOwn").html($("input[jns=txtUserInput]").val());
                    }

                }
                else {
                    /*
                    Show DYM results
                    */
                    if (dtItems.NewDataSet.DYM != null) {
                        $("#divDYM").show();
                        $("#LinkDYMResult").html(dtItems.NewDataSet.DYM.DYM_Words);
                        $("#LinkDYMResult").attr("name", dtItems.NewDataSet.DYM.DYM_Words);
                    }
                }
                $("#_mtableProduct").html(htmlProductTable);

            },
            //wat the weird wrong
            error: function (Msg) {
                alert('failed:' + Msg.status + ':' + Msg.responseText);
            }

        });
    });
});